// backend/main.go
package main

import (
	//    "net/http"
	"log"
	"wardrobe-server/data"
	"wardrobe-server/handlers"

	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()

	// 允许跨域（前端 Vite 默认跑在 5173）
	r.Use(func(c *gin.Context) {
		c.Header("Access-Control-Allow-Origin", "http://localhost:5173")
		c.Header("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS")
		c.Header("Access-Control-Allow-Headers", "Content-Type")
		if c.Request.Method == "OPTIONS" {
			c.AbortWithStatus(204)
			return
		}
		c.Next()
	})

	// 初始化 DuckDB，并注册路由到 handlers
	if err := data.Init("/Users/wuling/wardrobe.db"); err != nil {
		log.Fatalf("failed to initialize database: %v", err)
	}

	handlers.RegisterWardrobeRoutes(r)

	// 启动服务
	r.Run(":8080")
}
